package leetcode;

import java.util.*;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 删除数组中的重复项
 * @date 2022/5/24 8:18
 */
public class Code26 {


    public <T> T getCode(Class<T> tClass) throws InstantiationException, IllegalAccessException {
        T t = tClass.newInstance();
        return t;
    }

    public int removeDuplicates(int[] nums) {
        byte a = 121;
        byte b = 4;
        b = (byte) (a+ b);
        StringBuffer s = new StringBuffer(nums.length);
        s.append(a);

        Set<Integer> object = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            object.add(nums[i]);
        }
        List<Integer> list = new ArrayList<>();
        // 遍历set集合
        Iterator<Integer> iterator = object.iterator();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
        return list.size();
    }

    /**
     * @description: 双指针法，满足空间为o(1)的设定
     * @author oyz
     * @param
     * @date 2022/5/24 8:37
     * @version 1.0
     */
    public int removeDuplicates2(int[] nums) {
        int length = nums.length;
        if(length == 0){
            return 0;
        }
        int slow = 1,fast = 1;
        while(fast <length){
            if(nums[fast]!= nums[fast-1]){
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }



    public static void main(String[] args) {
       int [] nums = {1,1,1,2,2,3,3,4};
        System.out.println(new Code26().removeDuplicates(nums));
    }
}
